// xc_lshop/club/order/com/orderinfo.js
var actbill = require('../../model/club_bill')
var club_scanbill = require('../../model/club_scan')
Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },
  /**
   * 组件的初始数据
   */
  data: {
    actmodel: null,
    isshow: false,
    scanconfig: {},
    scanmode: "4",
    isoping: false,
    countdown: null,
    temtime: 100,
    countdownmode:'none'

  },
  ready() {
  
  },
  /**
   * 组件的方法列表
   */
  methods: {
    showmodel(actmodel, scanconfig, scanmode, isscan) {     
      var that = this;
      console.log("showmodel")
      console.log(actmodel)
      console.log(scanconfig)
      console.log(scanmode)
      console.log(isscan)

      var updatemodel = { actmodel, isshow: true, isoping: false }
      updatemodel["scanmode"] = scanmode
      updatemodel["scanconfig"] = (scanmode == 4 ? scanconfig["arrival"] : scanconfig["writeoff"])
      that.setData(updatemodel)
      this.autoop(actmodel, updatemodel["scanconfig"], scanmode)
    },
    autoop(actmodel, scanconfig, scanmode) {
      console.log("autoop")
      console.log(actmodel.status)
      console.log(scanmode)

      if (actmodel.status != scanmode) {
        this.setData({countdownmode:"next"})
        return
      }
      var dataset = {}
      dataset["id"] = actmodel["id"]
      //自动操作
      if (scanmode == actmodel.status) {
        if (scanconfig.autopost == 1) {
          this.setData({countdownmode:"none"})
          if (scanmode == "4") {
            dataset["op"] = "arrival"
          } else if (scanmode == "6") {
            dataset["op"] = "writeoff"
          }
          this.optool({ currentTarget: { dataset } })
        }
      }
    },

    quickcameraset(e) {
      var dataset = e.currentTarget.dataset
      var scanconfig = e.currentTarget.scanconfig
      var scanmode = this.data.scanmode
      var actfield = scanmodetostr(scanmode)
      var newval = -scanconfig[dataset.field]
      var updatemodel = {}
      updatemodel['scanconfig.' + dataset.field] = newval
      this.setData(updatemodel)
      switch (dataset.field) {
        case "autoscan": {
          break;
        }
        case "autopost": {
          break;
        }
      } 
    },

    scanmodetostr(scanmodel) {
      var actfield = ""
      if (scanmodel == "4") {
        actfield = "arrival"
      }
      else if (scanmodel == "6") {
        actfield = "writeoff"
      }
      return actfield
    },
    optool(e) {
      var dataset = e.currentTarget.dataset
      var that = this
      that.setData({ isoping: true }) 
      actbill.optool(dataset.op, dataset.id).then(res => {
        that.setData({ isoping: false })

        if (res["status"] == 1) {
          wx.showToast({
            title: '操作成功',
          })

          if (this.data.actmodel) {
            var udpatemdoel = {}
            switch (dataset.op) {
              case "arrival": {
                udpatemdoel["actmodel.status"] = "6"
                if (res["obj"]) {
                  udpatemdoel["actmodel.clubop"] = res["obj"];
                }
                break;
              }
              case "writeoff": {
                udpatemdoel["actmodel.status"] = "9"
                if (res["obj"]) {
                  udpatemdoel["actmodel.clubop.writeoffname"] = res["obj"]["writeoffname"];
                  udpatemdoel["actmodel.clubop.writeofftime"] = res["obj"]["writeofftime"];
                }
                break;
              }
            }
            var para = { op: "scanmode", scanmode: that.data.scanmode, model: that.data.actmodel }
            that.setData(udpatemdoel)
            that.triggerEvent("updatecomop", para)
           
          //自动扫码
            this.startcountdown();
          }
        }
        else {
          wx.showModal({
            title: res["message"] ? res["message"] : '操作失败'
          })
        }
      })
    },
    startcountdown() {
      var scanconfig = this.data.scanconfig
      if (scanconfig.autoscan == "1") {
        var opcountdown = this.selectComponent("#countdown")
        opcountdown.start(scanconfig.interval)
      }
      else
      {
        this.setData({countdownmode:"next"})
      }
    },
    closecountdown(){
    var  scanconfig= this.data.scanconfig
      if (scanconfig.autoscan == "1") {
      var opcountdown = this.selectComponent("#countdown")
      opcountdown.restcountdown()
      }
    },
    nextorder(){
      this.onPopClose();
      this.triggerEvent("opscan"),{}
      //opscan
    },
    onPopClose() {
      this.closecountdown();
      
      this.setData({ isshow: false })
    },
    xcopy(ev) {
      var dataset = ev.currentTarget.dataset
      var message = "复制成功"
      if (dataset.message) {
        message = dataset.message
      }
      wx.setClipboardData({
        data: dataset.val,
        success(res) {
          wx.showToast({
            title: message,
          })  
        }
      })  
    },
  }

})
